<%@ page pageEncoding="utf-8"%>
<%@ page
	import="java.lang.Throwable,java.sql.Connection,java.sql.SQLException,java.sql.Statement,java.util.ArrayList,java.util.HashMap,java.util.List,java.util.Map,org.jawa.core.util.*,org.jawa.core.database.*,java.io.File"%>
<%@ page import="org.jawa.admin.util.ParamUtils"%>
<%@ page import="org.jawa.core.JawaServer"%>
<%@page import="org.jawa.user.User"%>
<%@page import="org.jawa.user.UserManager"%>
<%@page import="org.apache.commons.codec.digest.DigestUtils"%>
<%
	// 系统已安装
	if (!JawaServer.getInstance().isSetupMode()) {
        response.sendRedirect("setup-completed.jsp");
        return;
    }
%>
<%!
    void setSetupFinished(HttpSession session) {
        JawaGlobals.setXMLProperty("setup","true");
    }
%>
<%
	// Get parameters //
	boolean save = ParamUtils.getBooleanParameter(request, "save");
	String username = ParamUtils.getParameter(request, "username");
	String email = ParamUtils.getParameter(request, "email");
	String password = ParamUtils.getParameter(request, "password");
	String passwordConfirm = ParamUtils.getParameter(request, "passwordConfirm");

	Map<String, String> errors = new HashMap<String, String>();

	if (save) {
		if (username == null) {
			errors.put("username", "账号不可以为空");
		} else {
			try {
				username = username.trim().toLowerCase();
			} catch (Exception se) {
				errors.put("username", "管理员不可以为空");
			}
		}
		
		if (password == null || password.trim().equals("")) {
			errors.put("password", "管理员密码不可以为空");
		}
		if (passwordConfirm == null) {
			errors.put("passwordConfirm", "请重复输入管理员密码");
		}
		if (password != null && passwordConfirm != null && !password.equals(passwordConfirm)) {
			errors.put("passwordMatch", "两次输入的密码不一致");
		}
		if (email == null || email.trim().equals("")) {
			errors.put("email", "请输入管理员EMAIL");
		}

		if (errors.size() == 0) {
			try {
				User adminUser = new User();
				adminUser.setUserName(username);
				adminUser.setLoginNo(username);
				adminUser.setUserType("1");
				adminUser.setUserStatus("0");
	            adminUser.setUserPassword(DigestUtils.md5Hex(password));
	            
	            if (email != null) {
	              adminUser.setUserEmail(email);
	            }
	            
	    		long userId = SequenceManager.nextID(JawaConstants.DB_USER_SEQ);
	    		adminUser.setUserId(userId);
	    		
	            UserManager.getInstance().createUser(adminUser);
	            
	            setSetupFinished(session);
	               
				response.sendRedirect("setup-finish.jsp");
				return;
			} catch (Exception e) {
				errors.put("general", "无法创建管理员账户!");
			}
		}
	}

	if (errors.isEmpty() && save) {
		response.sendRedirect("setup-finish.jsp");
		return;
	}
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>管理员账户</title>
		<meta name="currentStep" content="5" />
	</head>
	<body>
		<div class="row-fluid">
			<div class="page-header">
				<h3>
					管理员账户设置
				</h3>
			</div>
			<%
				if (errors.size() > 0) {
			%>
			<div class="alert alert-error fade in">
				<%=errors.get("general")!=null?errors.get("general"):"发生错误!"%>
			</div>
			<%
				}
			%>
			<div class="well">
				<form name="f" action="setup-admin-settings.jsp" method="post"
					class="form-horizontal">
					<input type="hidden" name="save" value="true">
					<div class="control-group  <%=errors.containsKey("username") ? "error" : ""%> ">
						<label for="focusedInput" class="control-label">
							管理员:
						</label>
						<div class="controls">
							<input type="text" name="username" size="30" value="<%=username!=null?username:"" %>" maxlength="75" id="usernametf" autocomplete="off" />
							<%
								if (errors.get("username") != null) {
							%>
							<span class="help-inline"> <%=errors.get("username")%></span>
							<%
								}
							%>
						</div>
					</div>
					<div class="control-group  <%=errors.containsKey("email") ? "error" : ""%> ">
						<label for="focusedInput" class="control-label">
							EMAIL:
						</label>
						<div class="controls">
							<input type="text" name="email" size="30" value="<%=email!=null?email:"" %>"  maxlength="75" id="emailtf" />
							<%
								if (errors.get("email") != null) {
							%>
							<span class="help-inline"> <%=errors.get("email")%></span>
							<%
								}
							%>								
						</div>
					</div>
					<div class="control-group  <%=errors.containsKey("password") ? "error" : ""%> ">
						<label for="focusedInput" class="control-label">
							密码:
						</label>
						<div class="controls">
							<input type="password" name="password" value="" size="20" maxlength="10" id="passtf" />
							<%
								if (errors.get("password") != null) {
							%>
							<span class="help-inline"> <%=errors.get("password")%></span>
							<%
								}
							%>								
						</div>
					</div>
					<div class="control-group  <%=errors.containsKey("passwordConfirm") ? "error" : ""%> ">
						<label for="focusedInput" class="control-label">
							重复密码:
						</label>
						<div class="controls">
							<input type="password" name="passwordConfirm" value="" size="20" maxlength="10" id="confpasstf" />
							<%
								if (errors.get("passwordConfirm") != null) {
							%>
							<span class="help-inline"> <%=errors.get("passwordConfirm")%></span>
							<%
								}
							%>
						</div>
					</div>

					<div class="form-actions" >
						<input type="button"  value="上一步" id="jawa-setup-prev" class="btn btn-info "  onclick="window.location.href='setup-mail-settings.jsp'" />
						<input type="submit" name="save" value="完成" id="jawa-setup-next" class="btn btn-primary " />
					</div>
					
				</form>
			</div>
		</div>
	</body>
</html>
